/**
 * 
 */
package net.toocruel.iqismart.service.core.impl;


import net.toocruel.iqismart.dao.UserRepository;
import net.toocruel.iqismart.dao.core.*;
import net.toocruel.iqismart.entity.constants.Action;
import net.toocruel.iqismart.entity.core.Credit;
import net.toocruel.iqismart.entity.core.Exchange;
import net.toocruel.iqismart.entity.core.Good;
import net.toocruel.iqismart.entity.rbac.User;
import net.toocruel.iqismart.security.core.support.Result;
import net.toocruel.iqismart.service.UserService;
import net.toocruel.iqismart.service.core.GoodService;
import net.toocruel.iqismart.service.dto.GoodsExchangeDTO;
import net.toocruel.iqismart.service.exception.BusinessException;
import net.toocruel.iqismart.service.exception.GoodsNotFoundException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.List;

/**
 * @author toocruel
 *
 */
@Service
@Transactional
public class GoodServiceImpl implements GoodService {
    @Autowired
    private TagRepository tagRepository;

    @Autowired
    private CategoryRepository categoryRepository;

    @Autowired
    private SupportRepository supportRepository;

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private AnswerRepository answerRepository;

    @Autowired
    private CommentRepository commentRepository;

    @Autowired
    private ArticleRepository articleRepository;

    @Autowired
    private GoodRepository goodRepository;

    @Autowired
    private ExchangeRepository exchangeRepository;

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserService userService;


    @Override
    public Page<Good> findAll(PageRequest pageRequest){
        return goodRepository.findAll(pageRequest);
    }

    @Override
    public List<Exchange> getExchanges(int i) {
        return exchangeRepository.findAll(new PageRequest(0,i,new Sort(Sort.Direction.DESC,"id"))).getContent();
    }

    @Override
    public Good findById(Long id) {
        Good good = goodRepository.findOne(id);
        if(good ==null){
            throw new GoodsNotFoundException();
        }
        return good;
    }

    @Override
    public Result exchange( GoodsExchangeDTO exchangeDTO, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            List<FieldError> fieldErrors = bindingResult.getFieldErrors();
            throw new BusinessException(fieldErrors);
        }
        Good good = goodRepository.findOne(exchangeDTO.getGoodId());
        User user = userRepository.findOne(exchangeDTO.getUser().getId());
        if(user.getData().getCoin()<good.getCoin()){
            throw new BusinessException("金币不足，无法兑换");
        }
        Credit credit = new Credit();
        credit.setUser(user);
        credit.setAction(Action.EXCHANGE);
        credit.setCoin(-good.getCoin());
        userService.addCredit(credit);

        Exchange exchange = new Exchange();
        exchange.setUser(user);
        exchange.setGood(good);
        exchange.setCoin(good.getCoin());
        exchange.setEmail(exchangeDTO.getEmail());
        exchange.setPhone(exchangeDTO.getPhone());
        exchange.setComment(exchangeDTO.getComment());
        exchange.setRealName(exchangeDTO.getRealName());
        exchangeRepository.save(exchange);


        return Result.SUCCESS;
    }
}
